package com.example.module_home.View;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.example.module_base.Base.BaseActivity;
import com.example.module_home.Module.CodeBank.CodeBankManager;
import com.example.module_home.Public.Constant;
import com.example.module_home.R;
import com.example.module_home.Utils.DialogUtil;
import com.example.module_home.databinding.HomeActivityLoginBinding;

/**
 * Created by Administrator on 2018/4/24 0024.
 */

public class HomeLoginActivity extends BaseActivity<HomeActivityLoginBinding> implements View.OnClickListener{
    private static final String TAG = "HomeLoginActivity";
    private String mUser;
    private String mPassword;
    private HomeActivityLoginBinding mBinding;
    private SharedPreferences mSharedPref;

    private boolean mIsRemember;
    @Override
    public int setLayout() {
            return R.layout.home_activity_login;
    }

    @Override
    public void init(HomeActivityLoginBinding bingding) {
        mBinding = bingding;
        checkRememberPassword();
        bingding.setOnClickListener(this);
        mBinding.rememberPasword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //存储是否记住密码状态
                mIsRemember = isChecked;
            }
        });
    }

    /**
     * 初始化检查之前是否记住过密码
     */
    private void checkRememberPassword() {
        mSharedPref = getSharedPreferences(Constant.SAVE_USER_INFO, Context.MODE_PRIVATE);
        String userName = mSharedPref.getString(Constant.USER_NAME, "");
        String userPassword = mSharedPref.getString(Constant.USER_PASSWORD, "");
        boolean isRemember = mSharedPref.getBoolean(Constant.IS_REMEMBER, false);
        if(isRemember){
            mBinding.loginAccount.setText(userName);
            mBinding.loginPassword.setText(userPassword);
            mBinding.rememberPasword.setChecked(true);
        }else {
            mBinding.rememberPasword.setChecked(false);
        }
        mIsRemember = isRemember;
    }

    private void loadUserInfo() {
        CodeBankManager.getInstance(this).getLoginUserInfo(mUser, mPassword, DialogUtil.showLoadingDialog(this),new CodeBankManager.CodeBankBack<String>() {
            @Override
            public void successful(String s) {
                if(s==null){
                    toast("帐号或密码错误，请重试");
                }else {
                    toast("登陆成功");
                    Intent intent = new Intent(HomeLoginActivity.this,HomeMainActivity.class);
                    intent.putExtra(Constant.USER_NAME,mUser);
                    intent.putExtra(Constant.USER_PASSWORD,mPassword);
                    intent.putExtra(Constant.IS_REMEMBER,mIsRemember);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void failed(int code, String e) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        int i=v.getId();
        if (i == R.id.login_button) {
            mUser=mBinding.loginAccount.getText().toString();
            mPassword=mBinding.loginPassword.getText().toString();
            loadUserInfo();
        }
    }

}
